import { create, StateCreator } from 'zustand';
import { devtools } from 'zustand/middleware';
import { immer } from 'zustand/middleware/immer';
import type { State,CustomRecord,Page,Form } from './types';
export type Actions = {
  getDataList(): void; // 执行查询
  onPageChange(pagination: Page): void; // 分页、排序、筛选变化时触发
  setQueryForm(form: Form): Promise<void>; // 查询表单
};
export type Store = State<CustomRecord> & Actions;
export const createStore: StateCreator<Store, [['zustand/immer', never]]> = (set, get) => ({
  queryForm: {}, // 搜索表单
  table: {
    isLoading: false,
    total: 0,
    data: [],
    pagination: { current:1,pageSize:10,total:0},
  },
  // 表格数据
  getDataList:  async () => {
    set((state) => {
      state.table.data = [
        {
          saleNetworkCode:"YGTP20240715123",
          contractCode:"自评",
          contractStatus:"员工绩效自评",
          contractStatusText:"2024-07-15",
          deliveryMonth:"待处理",
          payType:"关于员工德体制全面发展",
        },
        {
          saleNetworkCode:"YGTP20240715123",
          contractCode:"考核",
          contractStatus:"员工绩效统评",
          contractStatusText:"2024-07-15",
          deliveryMonth:"审批中",
          payType:"关于员工德体制全面发展",
        },
        {
          saleNetworkCode:"YGTP20240715123",
          contractCode:"审核",
          contractStatus:"员工加减分申请",
          contractStatusText:"2024-07-15",
          deliveryMonth:"已办结",
          payType:"关于员工德体制全面发展",
        },
        {
          saleNetworkCode:"YGTP20240715123",
          contractCode:"自评",
          contractStatus:"员工绩效多维测评",
          contractStatusText:"2024-07-15",
          deliveryMonth:"审批中",
          payType:"关于员工德体制全面发展",
        },
        {
          saleNetworkCode:"YGTP20240715123",
          contractCode:"自评",
          contractStatus:"员工加减分申请",
          contractStatusText:"2024-07-15",
          deliveryMonth:"审批中",
          payType:"关于员工德体制全面发展",
        },
      ];
      state.table.total = 5;
    });
    // set((state) => {
    //   state.table.isLoading = true;
    // });
    // const pageRequest = pick(get().table.pagination, ['current', 'pageSize']);
    // try {
    //   const params={
    //     ...get().queryForm, ...pageRequest,
    //   }
    //   const { data } = await axios.post('/eb/tTpmof/queryYyTPMOF02Full', params);
    //   if(data.data&&data.data.length>0){
    //     data.data.forEach((item:CustomRecord,index:number)=>{
    //       item.keyId=''+new Date().getTime()+index
    //     })
    //   }
    //   set((state) => {
    //     state.table.isLoading = false;
    //     state.table.data = data.data;
    //     state.table.total = data.total;
    //   });
    // } catch (error) {
    //   set((state) => {
    //     state.table.isLoading = false;
    //   });
    // }
  },
  // 分页
  onPageChange: (pagination : Page) => {
    set((state) => {
      state.table.pagination = pagination;
    });
    get().getDataList();
  },
  // 点击查询
  setQueryForm: async (form) => {
    set((state) => {
      state.queryForm = form;
      state.table.pagination={...state.table.pagination,current:1,pageSize:10}
    });
    get().getDataList();
  },
});

export const useStore = create<Store>()(devtools(immer<Store>(createStore)));